package per.jerry.flightinformation.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import per.jerry.flightinformation.entity.FlightTickets;
import per.jerry.flightinformation.entity.Flights;
import per.jerry.flightinformation.service.FlightService;

@Controller
public class FlightController {
	@Autowired
	private FlightService flightService;
	
	/**
	 * 跳转到航班列表页，查询航班信息
	 */
	@RequestMapping("/flightList.html")
	public ModelAndView gotoFlightList(String date,String takeName,String landingName){
		ModelAndView mv = new ModelAndView();
		//设置跳转页面_配置的为逻辑视图名，通过视图解析器转换成真实视图路径
		mv.setViewName("flights");
		//获取列表数据
		
		List<Flights> flights = flightService.findFlightsByParams(date, takeName, landingName);
		mv.addObject("flights", flights);
		mv.addObject("date", date);
		mv.addObject("takeName", takeName);
		mv.addObject("landingName", landingName);
		return mv; 
	}
	
	@RequestMapping(value="/flightTickets.json")
	//将返回值对象直接响应给用户
	@ResponseBody
	public String getFlightTickets(int flightId){
		List<FlightTickets> flightTickets = flightService.findTicketsByFlightId(flightId);
		return JSON.toJSONString(flightTickets);
	}

	public FlightService getFlightService() {
		return flightService;
	}

	public void setFlightService(FlightService flightService) {
		this.flightService = flightService;
	}
}
